package com.ruoyi.web.controller.repository;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.repository.domain.Milktea;
import com.ruoyi.system.repository.service.IMilkteaService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 奶茶Controller
 *
 * @author qiang
 * @date 2020-12-24
 */
@RestController
@RequestMapping("/repository/milktea")
public class MilkteaController extends BaseController {
    @Autowired
    private IMilkteaService milkteaService;

    /**
     * 查询奶茶列表
     */
    @PreAuthorize("@ss.hasPermi('repository:milktea:list')")
    @GetMapping("/list")
    public TableDataInfo list(Milktea milktea) {
        startPage();
        List<Milktea> list = milkteaService.selectMilkteaList(milktea);
        return getDataTable(list);
    }

    /**
     * 导出奶茶列表
     */
    @PreAuthorize("@ss.hasPermi('repository:milktea:export')")
    @Log(title = "奶茶", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Milktea milktea) {
        List<Milktea> list = milkteaService.selectMilkteaList(milktea);
        ExcelUtil<Milktea> util = new ExcelUtil<Milktea>(Milktea.class);
        return util.exportExcel(list, "milktea");
    }

    /**
     * 获取奶茶详细信息
     */
    @PreAuthorize("@ss.hasPermi('repository:milktea:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(milkteaService.selectMilkteaById(id));
    }

    /**
     * 新增奶茶
     */
    @PreAuthorize("@ss.hasPermi('repository:milktea:add')")
    @Log(title = "奶茶", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Milktea milktea) {
        return toAjax(milkteaService.insertMilktea(milktea));
    }

    /**
     * 修改奶茶
     */
    @PreAuthorize("@ss.hasPermi('repository:milktea:edit')")
    @Log(title = "奶茶", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Milktea milktea) {
        return toAjax(milkteaService.updateMilktea(milktea));
    }

    /**
     * 删除奶茶
     */
    @PreAuthorize("@ss.hasPermi('repository:milktea:remove')")
    @Log(title = "奶茶", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(milkteaService.deleteMilkteaByIds(ids));
    }

    //首页“为你推荐”
    @GetMapping("public/recommend")
    public List<Milktea> recommend(){
        return milkteaService.recommend();
    }

    //根据id返回对应的奶茶
    @GetMapping("public/selectOneMilktea")
    public Milktea selectOneMilktea(String id){
        return milkteaService.selectMilkteaById(id);
    }

    //返回全部奶茶
    @GetMapping("public/selectAllMilktea")
    public List<Milktea> selectAllMilktea(){
        return milkteaService.selectAllMilktea();
    }

    //返回对应分类的奶茶
    @GetMapping("public/selectByType")
    public List<Milktea> selectByType(String type){
        return milkteaService.selectByType(type);
    }
    //返回所有种类的奶茶
    @GetMapping("public/selectAllByType")
    public List<List<Milktea>> selectAllByType(){
        List<List<Milktea>> res=new ArrayList<>();
        int n=milkteaService.countType();//返回奶茶的种类数
        for(int i=0;i<n;i++){
            List<Milktea> temp= selectByType(Integer.toString(i+1));
            res.add(temp);
        }
        return res;
    }

    @GetMapping("public/countType")
    public int countType(){
        return milkteaService.countType();
    }

    @GetMapping("public/countMilktea")
    public int countMilktea(){
        return milkteaService.countMilktea();
    }


}
